package com.hzsai.socket;


import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * hzsai 2019/11/6
 * usage: 在容器中生成SockerIOServer实例
 **/
@Configuration
public class SocketIOConfig {

    @Value("localhost")
    private String defaultHost;

    /**
     * 配置socketServer端口
     */
    @Value("${socketio.port:10471}")
    private int socketioPort;

    /**
     * 默认namespace
     */
    @Value("${socketio.namespace:/LK-xxxxx/ws}")
    private String defaultNamespace = "/LK-xxxxx/ws";

    /**
     * 在这里怎么验证呢，只知道写
     * @return
     */
    @Bean("socketIOServer")
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration config =
                new com.corundumstudio.socketio.Configuration();

        config.setHostname(defaultHost);
        config.setPort(socketioPort);
        final SocketIOServer server = new SocketIOServer(config);

        // Netty-socketIO默认namespace 是""，具体代码在：package com.corundumstudio.socketio.namespace
        SocketIONamespace defaultNamespace_2 = server.addNamespace(""); //default
        defaultNamespace_2.addListeners(SocketIOEventHandler.class);

        SocketIONamespace namespace = server.addNamespace(defaultNamespace);
        namespace.addListeners(SocketIOEventHandler.class);
        return server;
    }

    /**
     * 用于扫描Netty socketIO 的注解：@OnConnect @OnDisConnect @OnEvent
     * @param socketIOServer
     * @return
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer) {
        return new SpringAnnotationScanner(socketIOServer);
    }
}
